package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.common.RequirePermission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.domain.query.QueryObject;
import cn.wolfcode.rbac.domain.vo.PageResult;
import cn.wolfcode.rbac.domain.vo.R;
import cn.wolfcode.rbac.domain.vo.RoleVo;
import cn.wolfcode.rbac.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/role")
@CrossOrigin(allowCredentials = "true")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @GetMapping("/list")
    @RequirePermission({"角色分页列表","role:list"})
    public R list(QueryObject qo){
        PageResult<Role> pageResult = roleService.selectRoleByPage(qo);
        return R.ok(pageResult);
    }

    @GetMapping("/listAll")
    @RequirePermission({"角色列表","role:listAll"})
    public R listAll() {
        List<Role> roles = roleService.listAll();
        return R.ok("查询成功",roles);
    }

    @DeleteMapping("/delete/{id}")
    @RequirePermission({"角色删除","role:delete"})
    public R delete(@PathVariable Long id) {
        roleService.deleteRoleById(id);
        return R.ok("删除成功");
    }

    @GetMapping("/info/{id}")
    @RequirePermission({"角色信息","role:info"})
    public R info(@PathVariable Long id) {
        Role role = roleService.selectById(id);
        return R.ok("查询成功",role);
    }

    @GetMapping("/query/{id}")
    @RequirePermission({"员工角色信息","role:query"})
    public R query(@PathVariable Long id) {
        List<Role> roles = roleService.query(id);
        return R.ok("查询成功",roles);
    }

    @PostMapping("/saveOrUpdate")
    @RequirePermission({"角色保存或更新","role:saveOrUpdate"})
    public R saveOrUpdate(@RequestBody RoleVo roleVo) {
        roleService.saveOrUpdateRole(roleVo);
        return R.ok("保存或修改成功");
    }
}

